import {defineConfig, loadEnv} from 'vite'


import createVitePlugins from "./myVite/plugins/index"
import path from 'path'
import AutoImport from "unplugin-auto-import/vite"
import prismjs from 'vite-plugin-prismjs';

// https://vitejs.dev/config/
export default defineConfig(({mode, command}) => {
    const env = loadEnv(mode, process.cwd())
    const {VITE_APP_ENV, VITE_BASE_URL, VITE_APP_BASE_PREFIX} = env

    return {
        base: VITE_BASE_URL,
        plugins: [
            ...createVitePlugins(env, command === 'build'),
            AutoImport({
                // 自动导入公共库
                imports: ["vue", "vue-router", "pinia"], //自动引入vue的ref、toRefs、onmounted等，无需在页面中再次引入
                // 自己的模块的导入
                dirs: ['./src/api/'],
                // 解决 ts 类型丢失的问题
                dts: './src/auto-import.d.ts',
            }),
            prismjs({
                languages: 'all',
            }),
        ],
        // vite 相关配置
        server: {
            port: 3677,
            host: true,
            open: true,
            proxy: {
                '/dev-api': {
                    target: 'http://localhost:8081',
                    changeOrigin: true,
                    rewrite: (p) => p.replace(/^\/dev-api/, '')
                },
                '/api-file': {
                    target: 'http://localhost:8080/uploads',
                    changeOrigin: true,
                    rewrite: (p) => p.replace(/^\/api-file/, '')
                },
                '/prefix' : {
                    target: 'http://122.152.215.206:9000',
                    changeOrigin: true,
                    rewrite: (p) => p.replace(/^\/prefix/, '')
                }
            }
        },
        // 配置 @
        resolve: {
            // https://cn.vitejs.dev/config/#resolve-alias
            alias: {
                // 设置路径
                '~': path.resolve(__dirname, 'src'),
                '@': path.resolve(__dirname, './src'),
            },
            // https://cn.vitejs.dev/config/#resolve-extensions
            extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue'],
        },
        css: {
            preprocessorOptions: {
                scss: {
                    additionalData:
                        '@import "./src/assets/styles/variables.module.scss";',
                },
            },
        },
    }
})
